package com.menhai.personmanagement.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import com.menhai.personmanagement.entity.StatisticsOfChronicDiseases;

public class ExportSysStatisticsOfChronicDiseasesInfoExcel {
	public void export(List<StatisticsOfChronicDiseases> StatisticsOfChronicDiseases, ServletOutputStream fout, HttpServletResponse resp) {
		  try {
		   //创建工作簿
		   HSSFWorkbook workbook = new HSSFWorkbook();
		   //创建合并单元格对象
		   CellRangeAddress callRangeAddress = new CellRangeAddress(0, 0, 0, 10);//起始行,结束行,起始列,结束列(输出列-1)
		   //总标题
		   HSSFCellStyle headStyle = createCellStyle(workbook, (short) 16, true, true, false);
		   //时间
		   HSSFCellStyle timeStyle = createCellStyle(workbook, (short) 10, true, true, false);
		   //标题
		   HSSFCellStyle colStyle = createCellStyle(workbook, (short) 10, true, true, true);
		   //内容样式
		   HSSFCellStyle cellStyle = createCellStyle(workbook, (short) 10, false, true, true);
		   //创建工作表
		   HSSFSheet sheet = workbook.createSheet("慢病患者统计");
		   //加载合并单元格对象
		   sheet.addMergedRegion(callRangeAddress);
		   //设置默认列宽
		   sheet.setDefaultColumnWidth(15);
		   //创建行
		   //创建头标题行;并且设置头标题
		   HSSFRow row = sheet.createRow(0);
		   HSSFCell cell = row.createCell(0);
		   //加载单元格样式
		   cell.setCellStyle(headStyle);
		   //文件标题
		   cell.setCellValue("慢病患者统计");

		   HSSFRow rowsan = sheet.createRow(1);
		   //导出文件第2行 时间 同结束列一样
		   HSSFCell cellsan2 = rowsan.createCell(5);
		   cellsan2.setCellStyle(timeStyle);
		   cellsan2.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

		   //3.2创建列标题;并且设置列标题
		   HSSFRow row2 = sheet.createRow(2);
		   String[] titles = {"姓名","年龄","性别","身份证号","婚姻状况","联系电话","现住地址","来源","慢病信息","状态","慢病年限"};
		   for (int i = 0; i < titles.length; i++) {
		    HSSFCell cell2 = row2.createCell(i);
		    //加载单元格样式
		    cell2.setCellStyle(colStyle);
		    cell2.setCellValue(titles[i]);
		   }
		   //操作单元格;将用户列表写入excel
		   if (StatisticsOfChronicDiseases != null) {
		    int i = 1;
		    for (int j = 0; j < StatisticsOfChronicDiseases.size(); j++) {
		     //创建数据行,前面有两行,头标题行和列标题行+日期
		     HSSFRow row3 = sheet.createRow(j + 3);//从第4行开始输出
		     HSSFCell cell_0 = row3.createCell(0);
		     HSSFCell cell_1 = row3.createCell(1);
		     HSSFCell cell_2 = row3.createCell(2);
		     HSSFCell cell_3 = row3.createCell(3);
		     HSSFCell cell_4 = row3.createCell(4);
		     HSSFCell cell_5 = row3.createCell(5);
		     HSSFCell cell_6 = row3.createCell(6);
		     HSSFCell cell_7 = row3.createCell(7);
		     HSSFCell cell_8 = row3.createCell(8);
		     HSSFCell cell_9 = row3.createCell(9);
		     HSSFCell cell_10 = row3.createCell(10);
		     cell_0.setCellStyle(cellStyle);
		     cell_1.setCellStyle(cellStyle);
		     cell_2.setCellStyle(cellStyle);
		     cell_3.setCellStyle(cellStyle);
		     cell_4.setCellStyle(cellStyle);
		     cell_5.setCellStyle(cellStyle);
		     cell_6.setCellStyle(cellStyle);
		     cell_7.setCellStyle(cellStyle);
		     cell_8.setCellStyle(cellStyle);
		     cell_9.setCellStyle(cellStyle);
		     cell_10.setCellStyle(cellStyle);
		     cell_0.setCellValue(StatisticsOfChronicDiseases.get(j).getName());
		     cell_1.setCellValue(StatisticsOfChronicDiseases.get(j).getAge());
		     cell_2.setCellValue(StatisticsOfChronicDiseases.get(j).getSex());
		     cell_3.setCellValue(StatisticsOfChronicDiseases.get(j).getIdnumber());
		     cell_4.setCellValue(StatisticsOfChronicDiseases.get(j).getMarriage());
		     cell_5.setCellValue(StatisticsOfChronicDiseases.get(j).getTelephony());
		     cell_6.setCellValue(StatisticsOfChronicDiseases.get(j).getNewaddress());
		     cell_7.setCellValue(StatisticsOfChronicDiseases.get(j).getLocal());
		     cell_8.setCellValue(StatisticsOfChronicDiseases.get(j).getChronic());
		     cell_9.setCellValue(StatisticsOfChronicDiseases.get(j).getState());
		     cell_10.setCellValue(StatisticsOfChronicDiseases.get(j).getHbnx());
		    }
		   }
		   //输出
		   try {
		    //下载文件名
		    String fileName = "慢病患者统计";
		    //设置文件名以及格式
		    resp.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName + ".xls", "UTF-8"));
		    //写入输出出去
		    workbook.write(fout);
		    fout.flush();
		    fout.close();
		    workbook.close();
		   } catch (IOException e) {
		    e.printStackTrace();
		   } finally {
		    try {
		     if (workbook != null) {
		      workbook.close();
		     }
		     if (fout != null) {
		      fout.close();
		     }
		    } catch (IOException e) {
		     e.printStackTrace();
		    }
		   }
		   System.out.println("患者明细成功");
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
		 }
	 private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize, boolean flag, boolean flag1, boolean flag2) {
		  // TODO Auto-generated method stub
		  HSSFCellStyle style = workbook.createCellStyle();
			if (flag1) {
				style.setAlignment(HorizontalAlignment.CENTER);//水平居中
			}
			if (flag2) {
				style.setBorderBottom(BorderStyle.THIN); //下边框
				style.setBorderLeft(BorderStyle.THIN);//左边框
				style.setBorderTop(BorderStyle.THIN);//上边框
				style.setBorderRight(BorderStyle.THIN);//右边框
			}
			style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
			//创建字体
			HSSFFont font = workbook.createFont();
			//是否加粗字体
			if (flag) {
				//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
				font.setBold(true);
			}
		  font.setFontHeightInPoints(fontsize);
		  //加载字体
		  style.setFont(font);
		  return style;
		 }
}
